package com.flydog.mall.controller.pms;

import com.flydog.mall.common.api.CommonResult;
import com.flydog.mall.common.api.PageResponse;
import com.flydog.mall.dto.PmsProductAttributeCategoryItem;
import com.flydog.mall.dto.PmsProductAttributeCategoryParam;
import com.flydog.mall.entity.pms.PmsProductAttributeCategory;
import com.flydog.mall.service.pms.PmsProductAttributeCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author Cao
 * @description 商品属性分类
 * @create 2021-03-23
 */

@RestController
@RequestMapping("/productAttribute/category")
public class PmsProductAttributeCategoryController {

    @Autowired
    private PmsProductAttributeCategoryService categoryService;

    @GetMapping(value = "/{id}")
    CommonResult<PmsProductAttributeCategory> getById(@PathVariable("id") Long id) {
        return categoryService.getItem(id).map(CommonResult::success).orElseGet(CommonResult::failed);
    }

    @PostMapping(value = "create")
    CommonResult<Integer> create(@RequestBody PmsProductAttributeCategoryParam param) {
        return CommonResult.success(categoryService.create(param.getName()));
    }

    @GetMapping(value = "/list")
    CommonResult<PageResponse<PmsProductAttributeCategory>>
    list(@RequestParam(value = "pageNum", defaultValue = "0") Integer pageNum,
         @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize) {
        return CommonResult.success(categoryService.getList(pageSize, pageNum));
    }

    @GetMapping(value = "/listAll")
    CommonResult<List<PmsProductAttributeCategory>>
    listAll() {
        return CommonResult.success(categoryService.getList());
    }

    @PostMapping(value = "delete")
    CommonResult<Integer> delete(@RequestParam("ids") Long[] ids){
        return CommonResult.success(categoryService.delete(ids));
    }

    @PostMapping(value = "update/{id}")
    CommonResult<Integer> update(@PathVariable("id") Long id, @RequestBody PmsProductAttributeCategory category) {
        return CommonResult.success(categoryService.update(id, category.getName()));
    }

    @GetMapping(value = "/list/withAttr")
    CommonResult<List<PmsProductAttributeCategoryItem>>
    listWithChildren() {
        return CommonResult.success(categoryService.getListWithAttribute());
    }
}
